package com.tudulist.service.rest;

import javax.persistence.NoResultException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sun.jersey.api.core.InjectParam;
import com.tudulist.dao.UserDao;
import com.tudulist.model.User;
import com.tudulist.rest.resource.UserResource;

@Component
@Path("/user")
public class UserResourceServer implements UserResource {

	@InjectParam
	UserDao userDao;

	@GET
	@Path("{login}")
	@Produces(MediaType.APPLICATION_XML)
	@Transactional
	public User findUser(@PathParam("login") String login) throws NoResultException {
		return userDao.find(login);
	}

	@POST
	@Path("/add")
	@Consumes(MediaType.APPLICATION_XML)
	@Transactional
	public void updateUser(User user) {
		userDao.save(user);
	}

	@GET
	@Path("/currentUser/{username}")
	@Produces(MediaType.APPLICATION_XML)
	@Transactional
	public User getCurrentUser(@PathParam("username") String username) {//FIXME share security context
		return this.findUser(username);
	}

}
